﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using qr = Ois.Smart.Data.Dua.Interaction.QueryResult;
using System.IO;
using System.Diagnostics;

namespace BinaryStructProject
{
    public class Program
    {
        static void Main(string[] args)
        {
            byte[] serializedData;
            qr.QueryResult expected = qr.QueryResult.LoadXml(File.ReadAllText("SampleResult.xml", Encoding.GetEncoding("windows-1251")));

            Stopwatch sw = new Stopwatch();
            sw.Start();

            BinaryStructFormatter formatter = new BinaryStructFormatter();
            serializedData = formatter.Serialize<qr.QueryResult>(expected);

            Debug.Print("BinaryStruct Serialize Elapsed: {0}", sw.ElapsedMilliseconds);
            Debug.Print("Size: {0} bytes", serializedData.Length);
            sw.Restart();

            qr.QueryResult actual = formatter.Deserialize<qr.QueryResult>(serializedData);

            Debug.Print("BinaryStruct Deserialize Elapsed: {0}", sw.ElapsedMilliseconds);

            if (!string.Equals(expected.Name, actual.Name))
                throw new ApplicationException();
            if (!string.Equals(expected.Description, actual.Description))
                throw new ApplicationException();
            if (!IsEqualsItemsCol(expected.Objects.Items, actual.Objects.Items))
                throw new ApplicationException();
        }

        public static bool IsEqualsItems(qr.Item item1, qr.Item item2)
        {
            if (object.ReferenceEquals(item1, item2))
                return true;
            if (item1 == null || item2 == null)
                return false;
            return item1.Alias == item2.Alias && item1.Category == item2.Category && item1.CID == item2.CID &&
                item1.CName == item2.CName && item1.DV == item2.DV && item1.EndTime == item2.EndTime &&
                item1.Group == item2.Group && item1.ID == item2.ID && item1.Name == item2.Name && item1.NS == item2.NS &&
                item1.OldDV == item2.OldDV && item1.OldID == item2.OldID && item1.PID == item2.PID &&
                item1.PName == item2.PName && item1.RC == item2.RC && item1.RefName == item2.RefName &&
                item1.StartTime == item2.StartTime && item1.UName == item2.UName &&
                item1.Uom == item2.Uom;
        }

        public static bool IsEqualsItemsCol(IList<qr.Item> col1, IList<qr.Item> col2)
        {
            if (object.ReferenceEquals(col1, col2))
                return true;
            if (col1 == null || col2 == null)
                return false;
            if (col1.Count != col2.Count)
                return false;
            if (col1.Count == 0)
                return true;
            for (int i = 0; i < col1.Count; i++)
            {
                if (!IsEqualsItems(col1[i], col2[i]))
                    return false;
                if (!IsEqualsItemsCol(col1[i].Items, col2[i].Items))
                    return false;
            }
            return true;
        }
    }
}
